using System;
using GameFramework.Event;
using LS.Saving;
using Unity.VisualScripting;

namespace LS.VisualScripting
{
    [UnitShortTitle("DataNode Changed")]
    public sealed class DataNodeChanged : GFEventUnit<DataNodeChangedEventArgs>
    {
        protected override int EventId => DataNodeChangedEventArgs.EventId;
        protected override string HookName => nameof(DataNodeChanged);

        [DoNotSerialize]
        public ValueInput DataNodePath { get; set; }
        
        [DoNotSerialize]
        public ValueOutput NewValue { get; set; }
        
        [DoNotSerialize]
        public ValueOutput OldValue { get; set; }
        
        protected override void Definition()
        {
            base.Definition();
            DataNodePath = ValueInput<string>(nameof(DataNodePath), String.Empty);
            NewValue = ValueOutput<object>(nameof(NewValue));
            OldValue = ValueOutput<object>(nameof(OldValue));
        }

        protected override bool ShouldTrigger(Flow flow, DataNodeChangedEventArgs args)
        {
            string nodePath = flow.GetValue<string>(DataNodePath);
            return base.ShouldTrigger(flow, args) && args.NodeName == nodePath;
        }

        protected override void AssignArguments(Flow flow, DataNodeChangedEventArgs args)
        {
            base.AssignArguments(flow, args);
            flow.SetValue(NewValue, args.NewValue.GetValue());
            flow.SetValue(OldValue, args.OldValue.GetValue());
        }
    }
}